package comgutianyu.qihangjiaoyu.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import comgutianyu.qihangjiaoyu.R;
import comgutianyu.qihangjiaoyu.model.LiveClass;
import java.util.List;

public class LiveClassAdapter extends RecyclerView.Adapter<LiveClassAdapter.ViewHolder> {
    private List<LiveClass> liveClasses;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(LiveClass liveClass);
    }

    public LiveClassAdapter(List<LiveClass> liveClasses) {
        this.liveClasses = liveClasses;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_live_class, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LiveClass liveClass = liveClasses.get(position);
        holder.teacherImage.setImageResource(liveClass.getTeacherImage());
        holder.teacherName.setText(liveClass.getTeacherName());
        holder.liveTime.setText(liveClass.getLiveTime());
        holder.studentCount.setText(liveClass.getStudentCount() + "人已报名");
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(liveClass);
            }
        });
    }

    @Override
    public int getItemCount() {
        return liveClasses.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView teacherImage;
        TextView teacherName;
        TextView liveTime;
        TextView studentCount;

        ViewHolder(View view) {
            super(view);
            teacherImage = view.findViewById(R.id.teacherImage);
            teacherName = view.findViewById(R.id.teacherName);
            liveTime = view.findViewById(R.id.liveTime);
            studentCount = view.findViewById(R.id.studentCount);
        }
    }
} 